package com.mht.student.service;

import com.mht.student.entity.Student;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

@SuppressWarnings("all")
public class SudentScoreService {

    private List<Student> students;

    public SudentScoreService() {
        students = new ArrayList<>();
        loadStudentsFromFile("students.txt");
    }

    private void loadStudentsFromFile(String filename) {
        try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split(",");
                students.add(new Student(parts[0], parts[1], parts[2], Integer.parseInt(parts[3]), Integer.parseInt(parts[4]), Integer.parseInt(parts[5])));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /*
     * @Author wangfusheng
     * @date 2024-10-18 11:10:48
     * @description 根据单科成绩排序
     * @param null
     * @return null
     */

    public List<Student> sortStudentsBySubject(String subject) {
        return students.stream()
                .sorted((student1, student2) -> {
                    switch (subject) {
                        case "Java":
                            return Integer.compare(student2.getJavaScore(), student1.getJavaScore());
                        case "数学":
                            return Integer.compare(student2.getMathScore(), student1.getMathScore());
                        case "英语":
                            return Integer.compare(student2.getEnglishScore(), student1.getEnglishScore());
                        default:
                            return 0;
                    }
                })
                .collect(Collectors.toList());
    }
    // 根据总成绩排序
    public List<Student> sortStudentsByTotalScore() {
        return students.stream()
                .sorted(Comparator.comparingInt(Student::getTotalScore).reversed())
                .collect(Collectors.toList());
    }

    // 根据平均成绩排序
    public List<Student> sortStudentsByAverageScore() {
        return students.stream()
                .sorted(Comparator.comparingDouble(Student::getAverageScore).reversed())
                .collect(Collectors.toList());
    }

    // 查询每个课程的最高分、最低分
    public void printCourseStatistics(String course) {
        Optional<Student> maxStudent, minStudent;
        OptionalDouble avgStudent;
        switch (course) {
            case "Java":
                maxStudent = students.stream().max(Comparator.comparingInt(Student::getJavaScore));
                minStudent = students.stream().min(Comparator.comparingInt(Student::getJavaScore));
                avgStudent = students.stream().mapToDouble(Student::getJavaScore).average();
                break;
            case "数学":
                maxStudent = students.stream().max(Comparator.comparingInt(Student::getMathScore));
                minStudent = students.stream().min(Comparator.comparingInt(Student::getMathScore));
                avgStudent = students.stream().mapToDouble(Student::getMathScore).average();
                break;
            case "英语":
                maxStudent = students.stream().max(Comparator.comparingInt(Student::getEnglishScore));
                minStudent = students.stream().min(Comparator.comparingInt(Student::getEnglishScore));
                avgStudent = students.stream().mapToDouble(Student::getEnglishScore).average();
                break;
            default:
                return;
        }
        if (maxStudent.isPresent() && minStudent.isPresent()) {
            System.out.println("最高分学生: " + maxStudent.get());
            System.out.println("最低分学生: " + minStudent.get());
            System.out.println("平均成绩: " + avgStudent.getAsDouble());
        }
    }

    // 查询每个班级某门课程的优秀率和不及格率
    public void printClassCourseRates(String className, String course) {
        List<Student> classStudents = students.stream()
                .filter(student -> student.getClassName().equals(className))
                .collect(Collectors.toList());

        long excellentCount, failCount;
        switch (course) {
            case "Java":
                excellentCount = classStudents.stream().filter(student -> student.getJavaScore() >= 90).count();
                failCount = classStudents.stream().filter(student -> student.getJavaScore() < 60).count();
                break;
            case "数学":
                excellentCount = classStudents.stream().filter(student -> student.getMathScore() >= 90).count();
                failCount = classStudents.stream().filter(student -> student.getMathScore() < 60).count();
                break;
            case "英语":
                excellentCount = classStudents.stream().filter(student -> student.getEnglishScore() >= 90).count();
                failCount = classStudents.stream().filter(student -> student.getEnglishScore() < 60).count();
                break;
            default:
                return;
        }

        double excellentRate = (double) excellentCount / classStudents.size() * 100;
        double failRate = (double) failCount / classStudents.size() * 100;

        System.out.println("班级: " + className);
        System.out.println(course + "优秀率: " + excellentRate + "%");
        System.out.println(course + "不及格率: " + failRate + "%");
    }
}
